﻿#include "Graph2DSettings.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QStandardItemModel>
#include <QListWidget>
#include <QStandardItem>

#include "ElaText.h"
#include "ElaPivot.h"
#include "ElaTabWidget.h"
#include "ElaScrollPageArea.h"

#include "ElaCheckBox.h"
#include "ElaComboBox.h"
#include "ElaMessageButton.h"
#include "ElaMultiSelectComboBox.h"
#include "ElaPlainTextEdit.h"
#include "ElaProgressBar.h"
#include "ElaRadioButton.h"
#include "ElaScrollPageArea.h"
#include "ElaSlider.h"
#include "ElaSpinBox.h"
#include "ElaText.h"
#include "ElaToggleButton.h"
#include "ElaToggleSwitch.h"
#include "ElaListView.h"
#include "ElaScrollBar.h"
#include "ElaTheme.h"
#include "ElaLineEdit.h"
#include "ElaScrollArea.h"
#include "ElaDoubleSpinBox.h"
#include "InfomationCurve2DBasical.h"
#include "TextFileParser.h"
#include "EColorComboBox.h"

#include "BaseModule/IBaseSignal.h"

namespace
{
    void clearGridLayout(QGridLayout* layout)
    {
        while (layout->count() > 0) {
            QLayoutItem* item = layout->takeAt(0); // 获取并移除第一个项目  
            if (item) {
                QWidget* widget = item->widget();
                if (widget) {
                    widget->deleteLater(); // 释放内存  
                }
                delete item; // 删除布局项  
            }
        }
    }

}

namespace pst
{
    Graph2DSettings::Graph2DSettings(QWidget* parent, Cure2DType type)
        : T_BasePage(parent)
        , m_variableValueIndex(-1)
        , page2RowCount(0)
    {
        //设置基本布局属性
        //setFixedWidth(300);
        setContentsMargins(0, 0, 0, 0);
    }

    Graph2DSettings::~Graph2DSettings()
    {

    }

    void Graph2DSettings::setCurveList(const QList<QString>& curveList)
    {
    }

    QStandardItem* Graph2DSettings::addCurveToList(const QString& curveName)
    {
        auto model = (QStandardItemModel*)m_curveNameListView->model();
        auto item = new QStandardItem(curveName);
        item->setCheckable(true);   // 设置为可勾选  
        item->setCheckState(Qt::Checked);
        //model->rowCount() == 0 ? item->setCheckState(Qt::Checked) : item->setCheckState(Qt::Unchecked);
        //QBrush brush(Qt::green);     // 设置复选框文字颜色或背景颜色  
        //item->setForeground(brush); // 设置前景色  
        model->appendRow(item);
        return item;
    }

    void Graph2DSettings::clearCurveList()
    {
    }

    void Graph2DSettings::setCurrentFixedValueIndex(int index)
    {
        m_variableValueIndex = index;
    }

    int Graph2DSettings::getCurrentVariableValueIndex() const
    {
        return m_variableValueIndex;
    }

    void Graph2DSettings::updateUILayout()
    {
        _titleFontSize = 14;
        _contentWidth = 290;

        //添加选项卡UI 数据 显示
        addTopTabPivotUI();
        //添加曲线列表UI
        addCurveListUI();
        ////添加变量轴UI
        //addVariableAxistUI();
        //setVariableValue();
        ////固定值UI
        //addFiexedValueToUI();
        //setFixedValue();
        //// value UI
        //setValueColumnUI();
        // 再设定value列


    ////类别UI
    //addCategaryToUI();
    //setCategoryData();
        //setDataOptrationUI();


        _page1Layout->addStretch();
        _page1Layout->setContentsMargins(10, 10, 10, 0);

        //---------- 第二页内容
        _page2Layout = new QGridLayout(m_page2CentralWidget);
        addXAxisSettingUI();
        addYAxisSettingUI();
        //addCurvePropertyUI();
        addLegendPropertyUI();
        QSpacerItem* horizontalSpacer = new QSpacerItem(40, 20,
            QSizePolicy::Expanding, QSizePolicy::Expanding);
        _page2Layout->addItem(horizontalSpacer, ++page2RowCount, 0);
        _page2Layout->setContentsMargins(10, 10, 10, 0);




        addCentralWidget(m_tabWidget, true, true, 0);
    }

    void Graph2DSettings::setCategaryData(const QList<QString>& categaryList)
    {
        m_categoryValueList = categaryList;
    }

    QList<QString> Graph2DSettings::getCategaryData() const
    {
        return m_categoryValueList;
    }

    void Graph2DSettings::hideCurvePropertylPanel()
    {
        //m_variableAxisText->hide();
        //m_variableAxisValue->hide();

        //_fixedValueText->hide();
        //_fixedValueArea->hide();


        //_checkBoxNormalize->hide();
        //_checkBoxDB->hide();
        m_variableAxisValue->clear();
        m_fixedCombo.clear();
        clearGridLayout(_fixedValueLayoutH);
        m_valueDataValue->clear();
        _checkBoxNormalize->setCheckState(Qt::Unchecked);
        _checkBoxDB->setCheckState(Qt::Unchecked);
        _checkBoxNormalize->setEnabled(false);
        _checkBoxDB->setEnabled(false);
    }

    void Graph2DSettings::setCurvePropertyPanelState(const InfomationCurve2DBasical& info)
    {
        //ToDO清空面板
        // 变量轴 扫描参数
        //m_variableAxisText->show();
        //m_variableAxisValue->show();
        m_currentTempInfo = info;
        if (info.getReader() == nullptr)
        {
            //TODO 警告
            return;
        }
        //m_variableAxisValue->clear();

        QVector<QString> keyNameList = info.getReader()->getEachColNameOfKey();
        QVector<QVector<double>> keyValueList = info.getReader()->getEachColDataOfKey();
        int currentScanIndex = info.getScanParameterIndex();
        auto valueNameList = info.getReader()->getEachColNameOfValue();

        //有多个曲线被选择->只判断 扫描参数（变量轴），DB和归一化。
        // 固定值和value列，直接置空

        /*************** 固定值************/
        m_fixedCombo.clear();
        clearGridLayout(_fixedValueLayoutH);
        if (!info.getIsMuti())
        {
            //添加除去currentScanIndex外的所有key列
            for (int i = 0, index = 0; i < keyNameList.size(); ++i)
            {
                if (i == currentScanIndex)
                {
                    continue;
                }
                ElaText* _phiText2 = new ElaText(keyNameList[i], this);
                _phiText2->setTextPixelSize(_titleFontSize);
                ElaComboBox* _phiComboBox2 = new ElaComboBox(this);

                QVector<double> data = keyValueList[i];
                QStringList _phiComboList2;
                for (int j = 0; j < data.size(); ++j)
                {
                    _phiComboList2.append(QString::number(data[j]));
                }
                _phiComboBox2->addItems(_phiComboList2);

                _fixedValueLayoutH->addWidget(_phiText2, index, 0);
                _fixedValueLayoutH->addWidget(_phiComboBox2, index++, 1);
                m_currentFixedValueWidgets.append({ _phiText2, _phiComboBox2 });
                m_fixedCombo.append(_phiComboBox2);
                //connect(_phiComboBox2, QOverload<int>::of(&ElaComboBox::currentIndexChanged), this, &Graph2DSettings::slot_updateCurveData);
            }
            // 设置所有key列选中的值
            auto otherKeyData = info.getOtherKeyData(); //otherKeyData中不含扫描参数
            for (int i = 0; i < m_fixedCombo.size(); ++i)//m_fixedCombo.size() = otherKeyData.size()+1
            {
                m_fixedCombo[i]->setCurrentText(QString::number(otherKeyData[i]));
            }
        }

        disconnect(m_variableAxisValue, QOverload<int>::of(&ElaComboBox::currentIndexChanged),
            this, &Graph2DSettings::slot_variableAxisChanged);
        /*************** 变量轴************/
        m_variableAxisValue->clear();
        if (!info.getIsMuti())
        {
            // 设置变量轴
            // 先设置固定值，然后再设置变量轴（固定值包含变量轴）
            for (int i = 0, index = 0; i < keyNameList.size(); ++i)
            {
                auto getVariableAxisName = keyNameList[i];
                m_variableAxisValue->addItem(getVariableAxisName);
            }
            m_variableAxisValue->setCurrentIndex(currentScanIndex);
        }
        else
        {
            auto commanVariableAxisName = info.getCommonFixedNames();
            for (int i = 0, index = 0; i < commanVariableAxisName.size(); ++i)
            {
                m_variableAxisValue->addItem(keyNameList[i]);
            }
            //因为多个曲线比较时，使用的第一个曲线为基础，commanVariableAxisName中的个数与keyNameList不一定一致，因此不能使用下标
            auto scanName = keyNameList[currentScanIndex];
            m_variableAxisValue->setCurrentText(scanName);
        }
        m_lastVariableAxisText = m_variableAxisValue->currentText();

        connect(m_variableAxisValue, QOverload<int>::of(&ElaComboBox::currentIndexChanged),
            this, &Graph2DSettings::slot_variableAxisChanged);

        /*************** value区域************/
        //// 再设定value列

        disconnect(m_valueDataValue, QOverload<int>::of(&ElaComboBox::currentIndexChanged), this, &Graph2DSettings::slot_updateCurveData);
        m_valueDataValue->clear();
        if (!info.getIsMuti())//选择多条曲线的时候，没有
        {
            for (const auto& i : valueNameList)
            {
                m_valueDataValue->addItem(i);
            }
            m_valueDataValue->setCurrentIndex(info.getValueIndex());
        }
        connect(m_valueDataValue, QOverload<int>::of(&ElaComboBox::currentIndexChanged), this, &Graph2DSettings::slot_updateCurveData);


        /*************** 数据操作区域************/
        //if (!info.getIsMuti())//单选 2种状态，多选有3种状态
        //{
        //    _checkBoxDB->setTristate(false);
        //    _checkBoxNormalize->setTristate(false);
        //}
        //else
        //{
        //    _checkBoxDB->setTristate(true);
        //    _checkBoxNormalize->setTristate(true);
        //}
        // 设置DB
        _checkBoxDB->setEnabled(true);
        int isDB = info.getIsDB();
        if (isDB == -1)
        {
            _checkBoxDB->setCheckState(Qt::PartiallyChecked);
            _checkBoxDB->setTristate(true);
        }
        else if (isDB == 0)
        {
            _checkBoxDB->setCheckState(Qt::Unchecked);
            _checkBoxDB->setTristate(false);
        }
        else
        {
            _checkBoxDB->setCheckState(Qt::Checked);
            _checkBoxDB->setTristate(false);
        }
        // 归一化
        _checkBoxNormalize->setEnabled(true);
        int isNormal = info.getIsNormalized();
        if (isNormal == -1)
        {
            _checkBoxNormalize->setCheckState(Qt::PartiallyChecked);
            _checkBoxNormalize->setTristate(true);
        }
        else if (isNormal == 0)
        {
            _checkBoxNormalize->setCheckState(Qt::Unchecked);
            _checkBoxNormalize->setTristate(false);
        }
        else
        {
            _checkBoxNormalize->setCheckState(Qt::Checked);
            _checkBoxNormalize->setTristate(false);
        }

        /*************** 最后连接槽函数************/
        for (int i = 0; i < m_fixedCombo.size(); ++i)//m_fixedCombo.size() = otherKeyData.size()+1
        {
            connect(m_fixedCombo[i], QOverload<int>::of(&ElaComboBox::currentIndexChanged), this, &Graph2DSettings::slot_updateCurveData);
        }

    }

    QString Graph2DSettings::getFontFamilyString(const QString& font)
    {
        if (font == "宋体")
        {
            return "SimSun";
        }
        else if (font == "微软雅黑")
        {
            return "Microsoft YaHei";
        }
        return QString();
    }

    void Graph2DSettings::addTopTabPivotUI()
    {
        //选项卡切换标题
        ElaPivot* _pivot = new ElaPivot(this);
        _pivot->setPivotSpacing(12);
        _pivot->setMarkWidth(40);
        _pivot->appendPivot("数据");
        _pivot->appendPivot("显示");
        _pivot->setCurrentIndex(0);
        _pivot->setTextPixelSize(16);
        setCustomWidget(_pivot);
        //---------- 第一页内容
        m_page1CentralWidget = new QWidget(this);
        m_page2CentralWidget = new QWidget(this);
        //选项卡视图
        m_tabWidget = new ElaTabWidget(this);
        m_tabWidget->tabBar()->hide();
        m_tabWidget->addTab(m_page1CentralWidget, "");
        m_tabWidget->addTab(m_page2CentralWidget, "");
        connect(_pivot, &ElaPivot::pivotClicked, this, [=](int index) {
            m_tabWidget->setCurrentIndex(index);
            });

        _page1Layout = new QVBoxLayout(m_page1CentralWidget);
    }

    void Graph2DSettings::addCurveListUI()
    {
        ElaText* listText = new ElaText("曲线", this);
        listText->setTextPixelSize(_titleFontSize);
        m_curveNameListView = new QListView(this);
        m_curveNameListView->setFixedHeight(150);
        m_curveNameListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
        m_curveNameListView->setEditTriggers(QAbstractItemView::NoEditTriggers);
        QStandardItemModel* model = new QStandardItemModel(m_curveNameListView);
        //for (size_t i = 0; i < 10; i++)
        //{
        //    model->appendRow(new QStandardItem(QString("曲线%1").arg(i)));
        //}
        m_curveNameListView->setModel(model);
        ElaScrollBar* listViewFloatScrollBar = new ElaScrollBar(m_curveNameListView->verticalScrollBar(), m_curveNameListView);
        listViewFloatScrollBar->setIsAnimation(true);

        QVBoxLayout* listLayout = new QVBoxLayout();
        listLayout->addWidget(m_curveNameListView);

        _page1Layout->addWidget(listText);
        _page1Layout->addLayout(listLayout);

        //connect(m_curveNameListView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &Graph2DSettings::slot_curveNameClicked);

        //auto model = (QStandardItemModel*)m_curveNameListView->model();
        connect(model, &QStandardItemModel::itemChanged, [&](QStandardItem* item)
            {
                auto index = model->indexFromItem(item).row();
                emit signal_showHideCurve(QPair{ index, item->checkState() });
                //if (item->checkState() == Qt::Checked) {
                //    qDebug() << item->text() << "is checked.";
                //}
                //else {
                //    qDebug() << item->text() << "is unchecked.";
                //}
            });
        //m_curveNameListView->model()->sele
    }

    void Graph2DSettings::addVariableAxistUI()
    {
        //变量轴
        m_variableAxisText = new ElaText("变量轴", this);
        m_variableAxisText->setTextPixelSize(_titleFontSize);
        m_variableAxisValue = new ElaComboBox(this);
        _page1Layout->addWidget(m_variableAxisText);
        _page1Layout->addWidget(m_variableAxisValue);
        //connect(m_variableAxisValue, QOverload<int>::of(&ElaComboBox::currentIndexChanged),
        //    this, &Graph2DSettings::slot_variableAxisChanged);
    }

    void Graph2DSettings::setFixedValue()
    {
        //QPair<QString, QList<QString>> tttt;
        //tttt.first = "adadad";
        //tttt.second = { "adadad", "adadad", "adadad" };
        //m_FixedVariableValueList << tttt << tttt << tttt;
        for (int num = 0, index = 0; num < m_FixedVariableValueList.size(); num++)
        {
            if (num == m_variableValueIndex)
            {
                continue;
            }

            const auto& i = m_FixedVariableValueList[num];
            ElaText* _phiText2 = new ElaText(i.first, this);
            _phiText2->setTextPixelSize(_titleFontSize);
            ElaComboBox* _phiComboBox2 = new ElaComboBox(this);
            QStringList _phiComboList2{ i.second };
            _phiComboBox2->addItems(_phiComboList2);

            _fixedValueLayoutH->addWidget(_phiText2, index, 0);
            _fixedValueLayoutH->addWidget(_phiComboBox2, index++, 1);
            m_currentFixedValueWidgets.append({ _phiText2, _phiComboBox2 });
        }
    }

    void Graph2DSettings::setVariableValue()
    {
        for (const auto& i : m_FixedVariableValueList)
        {
            m_variableAxisValue->addItem(i.first);
        }
        m_variableAxisValue->setCurrentIndex(m_variableValueIndex);
    }

    void Graph2DSettings::addFiexedValueToUI()
    {
        //固定值
        _fixedValueText = new ElaText("固定值", this);
        _fixedValueText->setTextPixelSize(_titleFontSize);
        //固定值布局
        _fixedValueArea = new QWidget(this);
        _fixedValueArea->setMaximumWidth(_contentWidth);
        _fixedValueArea->setStyleSheet("background:transparent;");
        _fixedValueLayoutH = new QGridLayout(_fixedValueArea);
        ElaScrollArea* _elaScrollArea = new ElaScrollArea(this);
        _elaScrollArea->setWidget(_fixedValueArea);
        _elaScrollArea->setWidgetResizable(true);
        _elaScrollArea->setFixedHeight(100);
        _elaScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        _elaScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
        _page1Layout->addWidget(_fixedValueText);
        _page1Layout->addWidget(_elaScrollArea);
    }

    void Graph2DSettings::addCategaryToUI()
    {
        //类别
        ElaText* _categoryText = new ElaText("类别", this);
        _categoryText->setTextPixelSize(_titleFontSize);
        //固定值布局
        m_categryValue = new ElaComboBox(this);
        _page1Layout->addWidget(_categoryText);
        _page1Layout->addWidget(m_categryValue);
    }

    void Graph2DSettings::setCategoryData()
    {
        for (const auto& i : m_categoryValueList)
        {
            m_categryValue->addItem(i);
        }
    }

    void Graph2DSettings::setValueColumnUI()
    {
        m_valueText = new ElaText("value", this);
        m_valueText->setTextPixelSize(_titleFontSize);
        m_valueDataValue = new ElaComboBox(this);
        _page1Layout->addWidget(m_valueText);
        _page1Layout->addWidget(m_valueDataValue);
    }

    void Graph2DSettings::setDataOptrationUI()
    {
        _checkBoxNormalize = new ElaCheckBox("归一化", this);
        //_checkBoxNormalize->setTristate(true);
        _checkBoxDB = new ElaCheckBox("dB", this);
        //_checkBoxDB->setTristate(true);
        _checkBoxNormalize->setEnabled(false);
        _checkBoxDB->setEnabled(false);

        ElaScrollPageArea* _dateOperationArea = new ElaScrollPageArea(this);
        _dateOperationArea->setMaximumWidth(_contentWidth);
        _dateOperationArea->setFixedHeight(60);
        QGridLayout* _categoryLayoutH = new QGridLayout(_dateOperationArea);
        _categoryLayoutH->addWidget(_checkBoxNormalize, 0, 0);
        _categoryLayoutH->addWidget(_checkBoxDB, 0, 1);
        _page1Layout->addWidget(_dateOperationArea);

        connect(_checkBoxNormalize, &ElaCheckBox::clicked, this, &Graph2DSettings::slot_checkBoxNormalizeClicked);
        connect(_checkBoxDB, &ElaCheckBox::clicked, this, &Graph2DSettings::slot_checkBoxDBClicked);

    }

    void Graph2DSettings::addXAxisSettingUI()
    {
        ElaText* _axisX = new ElaText("横轴", this);
        _axisX->setTextPixelSize(_titleFontSize);
        //横轴标题
        ElaText* _titleHTextX = new ElaText("横轴标题", this);
        _titleHTextX->setTextPixelSize(_titleFontSize);
        _titleHEditX = new ElaLineEdit(this);
        _titleHEditX->setText("频率(GHz)");

        //字体
        ElaText* _fontFamilyTextX = new ElaText("字体", this);
        _fontFamilyTextX->setTextPixelSize(_titleFontSize);
        _fontFamilyComboBoxX = new ElaComboBox(this);
        QStringList _fontFamilyComboListX{
            "微软雅黑",
            "宋体",
        };
        _fontFamilyComboBoxX->addItems(_fontFamilyComboListX);

        //字号
        ElaText* _fontSizeTextX = new ElaText("字号", this);
        _fontSizeTextX->setTextPixelSize(_titleFontSize);
        _fontSizeInputX = new ElaSpinBox(this);

        //最小值
        ElaText* _minTextX = new ElaText("起点", this);
        _minTextX->setTextPixelSize(_titleFontSize);
        _minInputX = new CustomDoubleSpinBox(this);
        _minInputX->setDecimals(6);
        _minInputX->setMinimum(-100000000.0);
        _minInputX->setMaximum(100000000.0);

        //最大值
        ElaText* _maxTextX = new ElaText("终点", this);
        _maxTextX->setTextPixelSize(_titleFontSize);
        _maxInputX = new CustomDoubleSpinBox(this);
        _maxInputX->setDecimals(6);
        _maxInputX->setMinimum(-100000000.0);
        _maxInputX->setMaximum(100000000.0);

        //步长
        ElaText* _stepTextX = new ElaText("步长", this);
        _stepComboBoxX = new ElaComboBox(this);
        QStringList _stepComboListX{
            "自动",
            "手动",
        };
        _stepComboBoxX->addItems(_stepComboListX);
        _stepTextX->setTextPixelSize(_titleFontSize);
        _setpInputX = new CustomDoubleSpinBox(this);
        _setpInputX->setDecimals(6);
        _setpInputX->setMinimum(0.000000001);
        _setpInputX->setMaximum(100000000.0);

        //页面2的布局
        _page2Layout->addWidget(_axisX, page2RowCount, 0);
        _page2Layout->addWidget(_titleHTextX, ++page2RowCount, 0);
        _page2Layout->addWidget(_titleHEditX, page2RowCount, 1);
        _page2Layout->addWidget(_fontFamilyTextX, ++page2RowCount, 0);
        _page2Layout->addWidget(_fontFamilyComboBoxX, page2RowCount, 1);
        _page2Layout->addWidget(_fontSizeTextX, ++page2RowCount, 0);
        _page2Layout->addWidget(_fontSizeInputX, page2RowCount, 1);
        _page2Layout->addWidget(_minTextX, ++page2RowCount, 0);
        _page2Layout->addWidget(_minInputX, page2RowCount, 1);
        _page2Layout->addWidget(_maxTextX, page2RowCount, 2);
        _page2Layout->addWidget(_maxInputX, page2RowCount, 3);
        _page2Layout->addWidget(_stepTextX, ++page2RowCount, 0);
        _page2Layout->addWidget(_stepComboBoxX, page2RowCount, 1);
        _page2Layout->addWidget(_setpInputX, ++page2RowCount, 1);

        connect(_titleHEditX, &ElaLineEdit::textChanged,
            this, [this]() {emit signal_XAxisTitleNameChanged(_titleHEditX->text()); });
        connect(_fontFamilyComboBoxX, QOverload<int>::of(&ElaComboBox::currentIndexChanged),
            this, [this]() {emit signal_XAxisTitleFontFamilyChanged(
                getFontFamilyString(_fontFamilyComboBoxX->currentText())); });
        connect(_fontSizeInputX, QOverload<int>::of(&ElaSpinBox::valueChanged),
            this, [this]() {emit signal_XAxisTitleFontSizeChanged(_fontSizeInputX->value()); });
        connect(_minInputX, QOverload<double>::of(&ElaDoubleSpinBox::valueChanged),
            this, [this]() {emit signal_XAxisMinValueChanged(_minInputX->value()); });
        connect(_maxInputX, QOverload<double>::of(&ElaDoubleSpinBox::valueChanged),
            this, [this]() {emit signal_XAxisMaxValueChanged(_maxInputX->value()); });
        connect(_stepComboBoxX, QOverload<int>::of(&ElaComboBox::currentIndexChanged),
            this, [this](int index) {
                if (index == 0) {
                    _setpInputX->hide();
                    emit signal_XAxisStepValueChanged(-1);
                }
                else if (index == 1) {
                    _setpInputX->show();
                    emit signal_XAxisStepValueChanged(_setpInputX->value());
                }});
                connect(_setpInputX, QOverload<double>::of(&ElaDoubleSpinBox::valueChanged),
                    this, [this] {emit signal_XAxisStepValueChanged(_setpInputX->value()); });

                //_setpInputX->setValue(5);
                _setpInputX->hide();
                _stepComboBoxX->setCurrentIndex(0);
                _maxInputX->setValue(0.00002);
                _fontSizeInputX->setValue(12);
                _minInputX->setValue(0.00001);
                _fontFamilyComboBoxX->setCurrentIndex(0);
    }

    void Graph2DSettings::addYAxisSettingUI()
    {
        ElaText* _axisY = new ElaText("纵轴", this);
        _axisY->setTextPixelSize(_titleFontSize);
        //横轴标题
        ElaText* _titleHTextY = new ElaText("纵轴标题", this);
        _titleHTextY->setTextPixelSize(_titleFontSize);
        _titleHEditY = new ElaLineEdit(this);
        _titleHEditY->setText("场强E(V/m）");

        //字体
        ElaText* _fontFamilyTextY = new ElaText("字体", this);
        _fontFamilyTextY->setTextPixelSize(_titleFontSize);
        _fontFamilyComboBoxY = new ElaComboBox(this);
        QStringList _fontFamilyComboListY{
            "微软雅黑",
            "宋体",
        };
        _fontFamilyComboBoxY->addItems(_fontFamilyComboListY);

        //字号
        ElaText* _fontSizeTextY = new ElaText("字号", this);
        _fontSizeTextY->setTextPixelSize(_titleFontSize);
        _fontSizeInputY = new ElaSpinBox(this);

        //最小值
        ElaText* _minTextY = new ElaText("起点", this);
        _minTextY->setTextPixelSize(_titleFontSize);
        _minInputY = new CustomDoubleSpinBox(this);
        _minInputY->setDecimals(6);
        _minInputY->setMinimum(-100000000.0);
        _minInputY->setMaximum(100000000.0);

        //最大值
        ElaText* _maxTextY = new ElaText("终点", this);
        _maxTextY->setTextPixelSize(_titleFontSize);
        _maxInputY = new CustomDoubleSpinBox(this);
        _maxInputY->setDecimals(6);
        _maxInputY->setMinimum(-100000000.0);
        _maxInputY->setMaximum(100000000.0);

        //步长
        ElaText* _stepTextY = new ElaText("步长", this);
        _stepComboBoxY = new ElaComboBox(this);
        QStringList _stepComboListY{
            "自动",
            "手动",
        };
        _stepComboBoxY->addItems(_stepComboListY);
        _stepTextY->setTextPixelSize(_titleFontSize);
        _setpInputY = new CustomDoubleSpinBox(this);
        _setpInputY->setDecimals(6);
        _setpInputY->setMinimum(0.000000001);
        _setpInputY->setMaximum(100000000.0);

        //页面2的布局
        _page2Layout->addWidget(_axisY, ++page2RowCount, 0);
        _page2Layout->addWidget(_titleHTextY, ++page2RowCount, 0);
        _page2Layout->addWidget(_titleHEditY, page2RowCount, 1);
        _page2Layout->addWidget(_fontFamilyTextY, ++page2RowCount, 0);
        _page2Layout->addWidget(_fontFamilyComboBoxY, page2RowCount, 1);
        _page2Layout->addWidget(_fontSizeTextY, ++page2RowCount, 0);
        _page2Layout->addWidget(_fontSizeInputY, page2RowCount, 1);
        _page2Layout->addWidget(_minTextY, ++page2RowCount, 0);
        _page2Layout->addWidget(_minInputY, page2RowCount, 1);
        _page2Layout->addWidget(_maxTextY, page2RowCount, 2);
        _page2Layout->addWidget(_maxInputY, page2RowCount, 3);
        _page2Layout->addWidget(_stepTextY, ++page2RowCount, 0);
        _page2Layout->addWidget(_stepComboBoxY, page2RowCount, 1);
        _page2Layout->addWidget(_setpInputY, ++page2RowCount, 1);

        connect(_titleHEditY, &ElaLineEdit::textChanged,
            this, [this]() {emit signal_YAxisTitleNameChanged(_titleHEditY->text()); });
        connect(_fontFamilyComboBoxY, QOverload<int>::of(&ElaComboBox::currentIndexChanged),
            this, [this]() {emit signal_YAxisTitleFontFamilyChanged(
                getFontFamilyString(_fontFamilyComboBoxY->currentText())); });
        connect(_fontSizeInputY, QOverload<int>::of(&ElaSpinBox::valueChanged),
            this, [this]() {emit signal_YAxisTitleFontSizeChanged(_fontSizeInputY->value()); });
        connect(_minInputY, QOverload<double>::of(&ElaDoubleSpinBox::valueChanged),
            this, [this]() {emit signal_YAxisMinValueChanged(_minInputY->value()); });
        connect(_maxInputY, QOverload<double>::of(&ElaDoubleSpinBox::valueChanged),
            this, [this]() {emit signal_YAxisMaxValueChanged(_maxInputY->value()); });
        connect(_stepComboBoxY, QOverload<int>::of(&ElaComboBox::currentIndexChanged),
            this, [this](int index) {
                if (index == 0) {
                    _setpInputY->hide();
                    emit signal_YAxisStepValueChanged(-1);
                }
                else if (index == 1) {
                    _setpInputY->show();
                    emit signal_YAxisStepValueChanged(_setpInputY->value());
                }});
                connect(_setpInputY, QOverload<double>::of(&ElaDoubleSpinBox::valueChanged),
                    this, [this] {emit signal_YAxisStepValueChanged(_setpInputY->value()); });

                //_setpInputY->setValue(5);
                _setpInputY->hide();
                _stepComboBoxY->setCurrentIndex(0);
                _maxInputY->setValue(0.00002);
                _minInputY->setValue(0.00001);
                _fontSizeInputY->setValue(12);
                _fontFamilyComboBoxY->setCurrentIndex(0);
    }

    void Graph2DSettings::addCurvePropertyUI()
    {
        //曲线
        ElaText* curveText = new ElaText("曲线", this);
        curveText->setTextPixelSize(_titleFontSize);

        //曲线颜色
        ElaText* curveColor = new ElaText("曲线颜色", this);
        curveColor->setTextPixelSize(_titleFontSize);
        EColorComboBox* curveColorSelector = new EColorComboBox(this);
        curveColorSelector->appendPredefinedColors();
        curveColorSelector->setEnabled(false);
        //曲线粗细
        ElaText* curveWidth = new ElaText("曲线粗细", this);
        curveWidth->setTextPixelSize(_titleFontSize);
        ElaComboBox* curveWidthSelector = new ElaComboBox(this);
        QStringList curveWidthList = { "0.25","0.5","0.75","1","1.5","2.25","3","4.5","6" };
        curveWidthSelector->addItems(curveWidthList);
        curveWidthSelector->setEnabled(false);

        _page2Layout->addWidget(curveText, ++page2RowCount, 0);
        _page2Layout->addWidget(curveColor, ++page2RowCount, 0);
        _page2Layout->addWidget(curveColorSelector, page2RowCount, 1);
        _page2Layout->addWidget(curveWidth, ++page2RowCount, 0);
        _page2Layout->addWidget(curveWidthSelector, page2RowCount, 1);
    }

    void Graph2DSettings::addLegendPropertyUI()
    {
        //图例
        ElaText* legendText = new ElaText("图例", this);
        legendText->setTextPixelSize(_titleFontSize);

        // 字体
        ElaText* fontFamilyText = new ElaText("字体", this);
        fontFamilyText->setTextPixelSize(_titleFontSize);
        _legendFontFamilySelector = new ElaComboBox(this);
        QStringList _fontFamilyComboListX{
        "宋体",
        "微软雅黑",
        };
        _legendFontFamilySelector->addItems(_fontFamilyComboListX);
        connect(_legendFontFamilySelector, QOverload<int>::of(&ElaComboBox::currentIndexChanged),
            this, [this]() {emit signal_legendTitleFontFamilyChanged(_legendFontFamilySelector->currentText()); });

        // 字号
        ElaText* fontSizeText = new ElaText("字号", this);
        fontSizeText->setTextPixelSize(_titleFontSize);
        _legendFontSizeSelector = new ElaComboBox(this);
        QStringList fontSize;
        for (int i = 1; i < 100; ++i)
        {
            fontSize << QString::number(i);
        }
        _legendFontSizeSelector->addItems(fontSize);
        _legendFontSizeSelector->setCurrentText("8");
        connect(_legendFontSizeSelector, QOverload<int>::of(&ElaComboBox::currentIndexChanged),
            this, [this]() {emit signal_legendTitleFontSizeChanged(_legendFontSizeSelector->currentText().toInt()); });


        _page2Layout->addWidget(legendText, ++page2RowCount, 0);
        _page2Layout->addWidget(fontFamilyText, ++page2RowCount, 0);
        _page2Layout->addWidget(_legendFontFamilySelector, page2RowCount, 1);
        _page2Layout->addWidget(fontSizeText, ++page2RowCount, 0);
        _page2Layout->addWidget(_legendFontSizeSelector, page2RowCount, 1);
    }

    void Graph2DSettings::setFixedVariableValue(
        const QList<QPair<QString, QList<QString>>>& fixedValueList)
    {
        m_FixedVariableValueList = fixedValueList;
    }

    void Graph2DSettings::slot_variableAxisChanged(int index)
    {

        //设置回原值
        if (m_isAddCurve == true)
        {
            m_isAddCurve = false;
            return;
        }


        auto allSelectedItem = m_curveNameListView->selectionModel()->selectedIndexes();
        if (allSelectedItem.isEmpty())
        {
            return;
        }
        QList<QStandardItem*>  itemList;
        QStandardItemModel* model = (QStandardItemModel*)m_curveNameListView->model();
        for (const auto& i : allSelectedItem)
        {
            itemList.append(model->itemFromIndex(i));
        }

        if (itemList.size() < 1)
        {
            return;
        }
        QString _typeName = m_variableAxisValue->currentText();

        int _reuslt = 0;
        emit IBaseSignalInstance->signal_sendQuestionMessageToUI(_reuslt, "切换变量", QString("当前变量为“%1”，切换变量将在新图中展示。").arg(_typeName));
        if (QDialog::Accepted != _reuslt)
        {
            //设置回原值
            m_isAddCurve = true;
            m_variableAxisValue->setCurrentText(m_lastVariableAxisText);
            m_lastVariableAxisText = m_variableAxisValue->currentText();
            return;
        }


        // 根据当前选择数据生成一个新的tempInfo
        // 根据当前的需求，新生成的tempInfo只需要存储新的变量轴
        InfomationCurve2DBasical tempInfo;
        //if (itemList.size() > 1)
        //{
        //    tempInfo.setIsMuti(true);
        //}
        tempInfo.setScanParameterName(m_variableAxisValue->currentText());

        //tempInfo.setIsDB(_checkBoxDB->isChecked());
        //tempInfo.setIsNormalized(_checkBoxNormalize->isChecked());
        //tempInfo.setCurveLineAddress(m_currentTempInfo.getCurveLineAddress());
        //emit signal_updateCurveData(itemList, tempInfo);


        emit signal_generateANewGraphCurve(itemList, tempInfo);

        //设置回原值
        m_isAddCurve = true;
        m_variableAxisValue->setCurrentText(m_lastVariableAxisText);
        m_lastVariableAxisText = m_variableAxisValue->currentText();
    }

    //选中曲线后，判断所有选中曲线的信息，根据信息设置下面框的内内容
    void Graph2DSettings::slot_curveNameClicked()
    {
        //TODO 发送信号给graph，通知选中该曲线

        auto allSelectedItem = m_curveNameListView->selectionModel()->selectedIndexes();
        QList<QStandardItem*>  itemList;
        QStandardItemModel* model = (QStandardItemModel*)m_curveNameListView->model();
        for (const auto& i : allSelectedItem)
        {
            itemList.append(model->itemFromIndex(i));
        }
        emit signal_chosenItemsChanged(itemList);
    }

    void Graph2DSettings::slot_updateCurveData()
    {
        auto allSelectedItem = m_curveNameListView->selectionModel()->selectedIndexes();
        if (allSelectedItem.isEmpty())
        {
            return;
        }
        QList<QStandardItem*>  itemList;
        QStandardItemModel* model = (QStandardItemModel*)m_curveNameListView->model();
        for (const auto& i : allSelectedItem)
        {
            itemList.append(model->itemFromIndex(i));
        }
        // 根据当前选择数据生成一个新的tempInfo
        // 根据当前的需求，新生成的tempInfo只需要存储  归一化和db信息是否发生更改，其他信息从自身的Info获取
        InfomationCurve2DBasical tempInfo;
        if (itemList.size() > 1)
        {
            tempInfo.setIsMuti(true);
        }
        else //单选才需要记录固定值和value
        {
            //固定值
            QVector<double> fixedValue;
            for (auto i : m_fixedCombo)
            {
                fixedValue.append(i->currentText().toDouble());
            }
            //value
            auto currentValueIndex = m_valueDataValue->currentIndex();

            tempInfo.setOtherKeyData(fixedValue);
            tempInfo.setValueIndex(currentValueIndex);
        }
        tempInfo.setIsDB(_checkBoxDB->isChecked());
        tempInfo.setIsNormalized(_checkBoxNormalize->isChecked());
        tempInfo.setCurveLineAddress(m_currentTempInfo.getCurveLineAddress());
        emit signal_updateCurveData(itemList, tempInfo);
    }

    //界面点过之后，都变为2态
    void Graph2DSettings::slot_checkBoxNormalizeClicked()
    {
        _checkBoxNormalize->setTristate(false);
        slot_updateCurveData();
    }

    void Graph2DSettings::slot_checkBoxDBClicked()
    {
        _checkBoxDB->setTristate(false);
        slot_updateCurveData();
    }

    void Graph2DSettings::slot_xAxisRangeChange(double minValue, double maxValue)
    {
        _minInputX->blockSignals(true);
        _minInputX->setValue(minValue);
        _minInputX->blockSignals(false);
        _maxInputX->blockSignals(true);
        _maxInputX->setValue(maxValue);
        _maxInputX->blockSignals(false);
    }

    void Graph2DSettings::slot_yAxisRangeChange(double minValue, double maxValue)
    {
        _minInputY->blockSignals(true);
        _minInputY->setValue(minValue);
        _minInputY->blockSignals(false);
        _maxInputY->blockSignals(true);
        _maxInputY->setValue(maxValue);
        _maxInputY->blockSignals(false);
    }

}
